package com.ksd.quartz.util;

import com.ksd.common.utils.DateUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;


public class UnzipUtility {
    public static void unzip(String zipFilePath, String destDirectory) throws IOException {
        File destDir = new File(destDirectory);
        if (!destDir.exists()) {
            destDir.mkdirs();
        }
        try (ZipFile zipFile = new ZipFile(zipFilePath)) {
            Enumeration<? extends ZipEntry> entries = zipFile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = entries.nextElement();
                File entryDestination = new File(destDir, entry.getName());
                if (entry.isDirectory()) {
                    entryDestination.mkdirs();
                } else {
                    File parentDir = entryDestination.getParentFile();
                    if (parentDir != null &&!parentDir.exists()) {
                        parentDir.mkdirs();
                    }
                    try (InputStream inputStream = zipFile.getInputStream(entry);
                         FileOutputStream outputStream = new FileOutputStream(entryDestination)) {
                        byte[] buffer = new byte[1024];
                        int length;
                        while ((length = inputStream.read(buffer)) > 0) {
                            outputStream.write(buffer, 0, length);
                        }
                    }
                }
            }
        }

    }
    public static void main(String[] args) {
//        String filePath = "C:\\Users\\wuyun\\Desktop\\首页照片\\wuyun_test\\demo.zip";
        String filePath = "D:\\playservice\\uploadPath\\upload\\2025\\04\\15\\压缩包测试文件_20250415170934A001.zip";
//        String filePath = "D:\\playservice\\uploadPath\\upload\\2025\\04\\15\\压缩包测试文件.zip";
//        String synchFilePath = PlayServiceConfig.getSynchFile();
        String synchFilePath = "D:/playservice/synchFilePath"+File.separator+  DateUtils.dateTimeNow();

//        try {
//            unzip(filePath,synchFilePath);
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
        synchFilePath = "D:\\playservice\\synchFilePath\\20250415171941";
        List<File> apkFiles = findApkFiles(synchFilePath);
        for (File apkFile : apkFiles) {
            System.out.println(apkFile.getAbsolutePath());
        }
        List<File> readMeFiles = JsonUtil.findJsonFiles(synchFilePath);
        if (readMeFiles.size() > 0) {
            String absolutePath = readMeFiles.get(0).getAbsolutePath();
        }

    }

    public static List<File> findApkFiles(String directoryPath) {
        List<File> apkFiles = new ArrayList<>();
        File directory = new File(directoryPath);
        if (directory.exists() && directory.isDirectory()) {
            File[] files = directory.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (file.isFile() && file.getName().endsWith(".apk")) {
                        apkFiles.add(file);
                    } else if (file.isDirectory()) {
                        apkFiles.addAll(findApkFiles(file.getAbsolutePath()));
                    }
                }
            }
        }
        return apkFiles;
    }

//    public static void main(String[] args) {
//        String filePath = "path/to/your/file.json";
//        try {
//            String jsonContent = readFile(filePath);
//            JSONObject jsonObject = new JSONObject(jsonContent);
//
//            // 假设 JSON 文件中有一个名为 "exampleProperty" 的属性
//            String propertyValue = jsonObject.getString("exampleProperty");
//            System.out.println("属性值: " + propertyValue);
//        } catch (IOException e) {
//            System.err.println("读取文件时出错: " + e.getMessage());
//        }
//    }

    private static String readFile(String filePath) throws IOException {
        StringBuilder content = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                content.append(line);
            }
        }
        return content.toString();
    }

}